#include <stdio.h>
#include <string.h>

int road[1001][1001];
int min1[1001];
int min2[1001];
char flag[1001];
int n, m, x;
int i, j, k;
int ans;

void init()
{
    int a, b, c;
    scanf("%d%d%d", &n, &m, &x);
    for (i=1; i<=n; ++i)
        for (j=1; j<=n; ++j)
            road[i][j] = 1000000000;
    for (i=1; i<=m; ++i)
    {
        scanf("%d%d%d", &a, &b, &c);
        road[a][b] = c;
    }    
    road[x][x] = 0;
}    

void dijkstra(int *minpath)
{
    int minv, minpos;
    memset(flag, 0, sizeof(flag));
    flag[x] = 1;

    for (k=1; k<n; ++k)
    {
        minv = 1000000000;
        for (j=1; j<=n; ++j)
            if (flag[j]==0 && minv>road[x][j])
            {
                minv = road[x][j];
                minpos = j;
            }    
        j = minpos;
        minpath[j] = road[x][j];
        flag[j] = 1;
        for (i=1; i<=n; ++i)
            if (flag[i]==0 && road[x][j]+road[j][i]<road[x][i])
            {
                road[x][i] = road[x][j] + road[j][i];
            }        
    }    
    /*/ debug
    printf("debug path:\n");
    for (i=1; i<=n; ++i)
        printf("%d ", minpath[i]);
    printf("\n");//*/
}    
void count()
{
    ans = 0;
    for (i=1; i<=n; ++i)
    {
        j = min1[i]+min2[i];
        if (ans < j) ans = j;
    }    
    printf("%d\n", ans);
}    
void converse()
{
    for (i=1; i<=n; ++i)
        for (j=i+1; j<=n; ++j)
            {
                k = road[i][j];
                road[i][j] = road[j][i];
                road[j][i] = k;
            }    
}    
int main()
{
    init();
    dijkstra(min1);
    converse();
    dijkstra(min2);
    count();
    
    return 0;    
}    
